package com.eiisan.weixin.web.feign;

import com.eiisan.base.constant.GlobalConstant;
import com.eiisan.core.util.api.Result;
import com.eiisan.weixin.service.RedisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 微信VerifyCode服务feign实现类
 * @Author gragonfly
 * @Date 2019/6/21
 **/
@RestController
@Api(tags = "API - WeiXinVerifyCodeFeign")
public class WeiXinVerifyCodeFeign implements WeiXinVerifyCodeFeignApi {

    @Autowired
    private RedisService redisService;

    @Override
    @ApiOperation(httpMethod = "POST", value = "查询微信验证码是否正确")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "phone", dataType = "String", required = true, value = "用户手机号码"),
            @ApiImplicitParam(paramType = "query", name = "weixinCode", dataType = "String", required = true, value = "微信验证码")})
    public Result<Boolean> checkWeiXinVerifyCode(@RequestParam String phone, @RequestParam String weixinCode) {
        if (StringUtils.isEmpty(phone)) {
            return Result.errorRequest("手机号码不能为空");
        }
        if (StringUtils.isEmpty(weixinCode)) {
            return Result.errorRequest("验证码不能为空");
        }
        //1.查找根据手机号查询redis,返回对应注册码
        String redisCode = redisService.getKey(GlobalConstant.WeiXin.REGISTER_CODE_KEY + phone);
        if (StringUtils.isEmpty(redisCode)) {
            //验证码已过期
            return Result.success(Boolean.FALSE);
        }
        if (!redisCode.equals(weixinCode)) {
            //验证码不正确
            return Result.success(Boolean.FALSE);
        }
        //2.清空注册码
        redisService.deleteKey(GlobalConstant.WeiXin.REGISTER_CODE_KEY + phone);
        return Result.success(Boolean.TRUE);
    }
}
